#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
	vector<vector<int>> generateMatrix1(int n) {	
		int top = 0;
		int right = n - 1;
		int bottom = n - 1;
		int left = 0;
		int num = 1, k = n * n;
		vector<vector<int>> ans(n, vector<int>(n));
		while(num <= k){
			for(int i = left; i <= right; i++, num++) ans[top][i] = num;
			top++;
			for(int i = top; i <= bottom; i++, num++) ans[i][right] = num;
			right--;
			for(int i = right; i >= left; i--, num++) ans[bottom][i] = num;
			bottom--;
			for(int i = bottom; i >= top; i--, num++) ans[i][left] = num;
			left++;
		}
		return ans;
	}

};

int main(){
	Solution so;
	int n = 3;
	auto res = so.generateMatrix1(n);
	for(auto items : res){
		for(auto item : items){
			cout << item << ",";
		}
		cout << endl;
	}
	return 0;
}